/*=============================================================================

  APPLICATION MANAGER IO DEVICE INTERFACE MODULE

  FILE: SYSmgr_HAIO.cpp
  
  DESCRIPTION

    Appliacation Manager IO Interface Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================
	                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   IO Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <SYSmgr.h>
#include "HAIO_cfg.h"
#include <math.h>
#include <devSP/ioHal_emul.h>

/*=============================================================================
  LOCALIZED VARIABLE DEFINITION
=============================================================================*/
static dword aioDevEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser);

/*=============================================================================
  CLASS IMPLEMENTATION
=============================================================================*/
void SYSmgr::HAIO_Alloc(void)
{
	ObjClient_s		c;
	
	m_pAIO = new HAIO;
	
	m_aioInit = FALSE;
	m_aioCFG = NULL;

	c.pUserCxt = this;
	c.EventCbf = aioDevEventCallback;
	m_pAIO->SetClient(&c);

#ifdef TG_HW_AIODEV_SIMUL
	//m_pAIO->HAL_Register(DIODEV_EMULATOR, DioHal_Emul_Register, DioHal_Emul_Deregister);
#else
	//m_pAIO->HAL_Register(AIODEV_AIO, AioHal_AIO_Register, AioHal_AIO_Deregister);
#endif
}

void SYSmgr::HAIO_Free(void)
{
	if(m_pAIO == NULL)
		return;

	m_pAIO->Close();
	if(m_aioCFG)
	{
		memFree(m_aioCFG);
		m_aioCFG = NULL;
	}
	// Deregister device support package
#ifdef TG_HW_AIODEV_SIMUL
	//m_pAIO->HAL_Deregister(DIODEV_EMULATOR);
#else

	m_pAIO->HAL_Deregister(AIODEV_AIO);
#endif

	if(m_pAIO)
	{
		HAIO_Close();

		delete m_pAIO;
		m_pAIO = NULL;
	}
}

boolean SYSmgr::HAIO_Setup(char *pszFile)
{
	if(m_aioCFG)
	{
		memFree(m_aioCFG);
		m_aioCFG = NULL;
	}

	if(pszFile != NULL)
	{
		m_aioCFG = (char*)memCalloc(strlen(pszFile) + 1);
		strcpy(m_aioCFG, pszFile);
	}

	return	TRUE;
}

boolean SYSmgr::HAIO_Init(void)
{
	aiodev_cfg_s			*cfg;
	
	Event_Handler(HAIO_DEV, INIT_START_E, 0, 0);

	if(m_aioInit == TRUE)
	{
		Event_Handler(HAIO_DEV, INIT_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}

	// Check the configuration file
	cfg = aiodev_cfg_load(m_aioCFG);

	if(cfg == NULL)
	{
		LOGWARN("[aioDev] <%s> loading failure.", m_aioCFG);
		LOGWARN("[aioDev] Load the default configuration.");

		cfg = aiodev_default_alloc();

		if(aiodev_cfg_save(cfg, m_aioCFG) == FALSE)
		{
			aiodev_default_free(cfg);
			LOGERR("[aioDev] Default configuration save failure.");

			return	FALSE;
		}
		aiodev_default_free(cfg);
	}
	else
	{
		aiodev_cfg_free(cfg);
	}
	
	if(m_pAIO->Init() == TRUE)
	{
		m_pAIO->INIT_IO_STATE();
		
		m_aioInit = TRUE;
		Event_Handler(HAIO_DEV, INIT_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}
	Event_Handler(HAIO_DEV, ERR_INIT_E, HDEV_MAX, 0);

	return	FALSE;
}

boolean SYSmgr::HAIO_Close(void)
{
	Event_Handler(HAIO_DEV, CLOSE_START_E, 0, 0);

	if(m_aioInit == FALSE)
	{
		Event_Handler(HAIO_DEV, CLOSE_DONE_E, HDEV_MAX, 0);
		return	TRUE;
	}

	m_pAIO->INIT_IO_STATE();
	if(m_pAIO->Close() == TRUE)
	{
		m_aioInit = FALSE;
		Event_Handler(HAIO_DEV, CLOSE_DONE_E, 0, 0);
		return	TRUE;
	}
	m_aioInit = FALSE;
	Event_Handler(HAIO_DEV, ERR_CLOSE_E, 0, 0);

	return	TRUE;
}

/*=============================================================================
  LOCALIZED FUNCTION IMPLEMENTATION
=============================================================================*/
dword aioDevEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser)
{
	SYSmgr		*pCxt = (SYSmgr*)pUser;

	if(pCxt)
	{
		return	pCxt->HEvent_Handler(HAIO_DEV, dwEvent, wParam, lParam);
	}
	return	(0);
}



#if 0	
//---MANUAL
boolean SYSmgr::HAIO_LASERset(int32 headID/*PRM_HEAD_e*/, int32 laserPOS/*PRM_LASER_e*/, double64 val/*0~100%: 1%=0.05V*/)
{
	int32		ch = HAIO_CHMAX;
	
	if(m_aioInit == FALSE)
	{
		return	FALSE;
	}
	switch(headID)
	{
	case PRM_HEAD1:
	{
		switch(laserPOS)
		{
		case PRM_LSRL: ch = HAIO_CH0; break;
		case PRM_LSRR: ch = HAIO_CH1; break;
		}
		break;
	}
	case PRM_HEAD2:
	{
		switch(laserPOS)
		{
		case PRM_LSRL: ch = HAIO_CH2; break;
		case PRM_LSRR: ch = HAIO_CH3; break;
		}
		break;
	}
	}
	if(RANGE_IN(ch, HAIO_CH0, HAIO_CH3) == FALSE)
	{
		return	FALSE;
	}
	val = MAX(0.0, val);
	val = MIN(100.0,val);
	val = (val*0.01)*5.0;
	val = 5.0-val; // invert

	return	m_pAIO->AO(ch, val);
}


boolean SYSmgr::HAIO_LASERget(int32 headID, int32 laserPOS/*PRM_LASER_e*/, double64 *val)
{
	int32		ch = HAIO_CHMAX;
	double64		ao;
	
	if(m_aioInit == FALSE)
	{
		*val = 0.0;
		return	FALSE;
	}
	switch(headID)
	{
	case PRM_HEAD1:
	{
		switch(laserPOS)
		{
		case PRM_LSRL: ch = HAIO_CH0; break;
		case PRM_LSRR: ch = HAIO_CH1; break;
		}
		break;
	}
	case PRM_HEAD2:
	{
		switch(laserPOS)
		{
		case PRM_LSRL: ch = HAIO_CH2; break;
		case PRM_LSRR: ch = HAIO_CH3; break;
		}
		break;
	}
	}
	if(RANGE_IN(ch, HAIO_CH0, HAIO_CH3) == FALSE)
	{
		return	FALSE;
	}
	if(m_pAIO->AOget(ch, &ao) == FALSE)
	{
		*val = 0.0;
		return	FALSE;
	}
	*val = ((5.0-ao)/5.0)*100.0;
	*val = MIN((*val), 100.0);
	*val = MAX((*val), 0.0);

	return	TRUE;
}

//---AUTO
boolean SYSmgr::HAIO_LASER(boolean on)
{
	boolean			done = TRUE;
	int32			j;
	
	if(m_aioInit == FALSE)
	{
		return	FALSE;
	}
	if(on == TRUE)
	{
		SYSmgr_cfg_s			*cfg = CFG_Ptr();
		prm_cfg_s			*pc;
		PRMHEAD_s			*ph;

		pc = &cfg->prm_cfg;

#ifdef	TG_HW_FGDEV_SINGLE_SIMUL
		for(j=PRM_HEAD1; j<PRM_HEAD2; j++)
#else
		for(j=PRM_HEAD1; j<=PRM_HEAD2; j++)
#endif
		{
			//--GET.VALUE
			ph = &pc->pi_jobList[pc->pi_jobSEL].CAM[j];

			if(HAIO_LASERset(j, PRM_LSRL, ph->LASER[PRM_LSRL]) == FALSE)
			{
				done = FALSE;
			}
			if(HAIO_LASERset(j, PRM_LSRR, ph->LASER[PRM_LSRR]) == FALSE)
			{
				done = FALSE;
			}
		}
	}
	else
	{
#ifdef	TG_HW_FGDEV_SINGLE_SIMUL
		for(j=PRM_HEAD1; j<PRM_HEAD2; j++)
#else
		for(j=PRM_HEAD1; j<=PRM_HEAD2; j++)
#endif
		{
			if(HAIO_LASERset(j, PRM_LSRL, 0.0) == FALSE)
			{
				done = FALSE;
			}
			if(HAIO_LASERset(j, PRM_LSRR, 0.0) == FALSE)
			{
				done = FALSE;
			}
		}
	}

	return	done;
}
#endif

/*--- END OF SYSmgr_HAIO.CPP ---*/
